//
//  BaseTabBarViewController.m
//  MyFramework
//
//  Created by 余洋 on 2018/5/8.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "HomeViewController.h"
#import "TinyTaoViewController.h"
#import "MyViewController.h"
#import "CartViewController.h"
#import "MessageViewController.h"
#import "BaseNavigationViewController.h"

#import "AddTabBar.h"

@interface BaseTabBarViewController ()

@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设为不透明
    self.tabBar.translucent = NO;
    //背景颜色
    [self.tabBar setBarTintColor:[UIColor blackColor]];
    //文字颜色
    [self.tabBar setTintColor:[UIColor whiteColor]];
    
    HomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"HomeViewController"];
    TinyTaoViewController *tinyTaoVC = [[UIStoryboard storyboardWithName:@"TinyTao" bundle:nil] instantiateViewControllerWithIdentifier:@"TinyTaoViewController"];
//    MyViewController *myVC = [[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"MyViewController"];
    CartViewController *cartVC = [[UIStoryboard storyboardWithName:@"Cart" bundle:nil] instantiateViewControllerWithIdentifier:@"CartViewController"];
    MessageViewController *messageVC = [[UIStoryboard storyboardWithName:@"Message" bundle:nil] instantiateViewControllerWithIdentifier:@"MessageViewController"];
    
    [self addChildViewController:homeVC title:@"首页" norImage:[UIImage imageNamed:@"ic_tabbar_home_unsel"] selImage:[UIImage imageNamed:@"ic_tabbar_home_sel"]];
    [self addChildViewController:tinyTaoVC title:@"微淘" norImage:[UIImage imageNamed:@"ic_tabbar_boon_unsel"] selImage:[UIImage imageNamed:@"ic_tabbar_boon_sel"]];
    [self addChildViewController:messageVC title:@"消息" norImage:[UIImage imageNamed:@"ic_tabbar_message_unsel"] selImage:[UIImage imageNamed:@"ic_tabbar_message_sel"]];
    [self addChildViewController:cartVC title:@"购物车" norImage:[UIImage imageNamed:@"ic_tabbar_my_unsel"] selImage:[UIImage imageNamed:@"ic_tabbar_my_sel"]];
//    [self addChildViewController:myVC title:@"我的" norImage:[UIImage imageNamed:@"ic_tabbar_my_unsel"] selImage:[UIImage imageNamed:@"ic_tabbar_my_sel"]];
    // 设置自定义的tabbar
    [self setCustomtabbar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addChildViewController:(UIViewController *)childController title:(NSString*)title norImage:(UIImage*) norImage selImage:(UIImage*) selImage{
    //Tabbar
    UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:norImage selectedImage:[selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    childController.tabBarItem = tabBarItem;
    if (@available(iOS 10.0, *)) {
        //改变小红点颜色
        childController.tabBarItem.badgeColor = RGBA(0x983079, 1.0);
    } else {
        
    }
    BaseNavigationViewController* navController = [[BaseNavigationViewController alloc] initWithRootViewController:childController];
    childController.title = title;
    [self addChildViewController:navController];
}

- (void)setCustomtabbar{
    AddTabBar *tabbar = [[AddTabBar alloc]init];
    [self setValue:tabbar forKeyPath:@"tabBar"];
    [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)centerBtnClick:(UIButton *)btn{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击了中间按钮" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertView addAction:action];
    [self presentViewController:alertView animated:YES completion:nil];
}
@end
